﻿Public Class fListaArtbyNotaDev
    Dim dsConMov As lbGlobalcom.dsConsultaMovimi
    Dim daTipoSubProd As lbGlobalcom.dsGlobalTableAdapters.GLO_DATOSTABLASTableAdapter
    Private Sub OrganizaDatosTabla()
        Dim dv As DataView = dsConMov.INFONOTASCREDITO.DefaultView
        dv.Sort = "IDDOCUMENTO, NUMERO, PAREJA"
        dv.RowFilter = "SELECT=1"
        Dim Dt As DataTable = dv.ToTable
        dsConMov.INFONOTASCREDITO.Rows.Clear()
        dsConMov.INFONOTASCREDITO.Merge(Dt)
    End Sub
    Public Property DatosSelect() As lbGlobalcom.dsConsultaMovimi
        Get
            Return dsConMov
        End Get
        Set(ByVal value As lbGlobalcom.dsConsultaMovimi)
            CargaDatosSubProd()
            dsConMov = value
            INFONOTASCREDITODataGridView.DataSource = dsConMov
            INFONOTASCREDITODataGridView.DataMember = "INFONOTASCREDITO"
        End Set
    End Property

    Private Sub btnAcepta_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAcepta.Click
        INFONOTASCREDITODataGridView.EndEdit()
        OrganizaDatosTabla()
        Me.DialogResult = Windows.Forms.DialogResult.OK
    End Sub

    Private Sub btnSalir_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSalir.Click
        Me.DialogResult = Windows.Forms.DialogResult.Cancel
    End Sub
    Private Sub CargaDatosSubProd()
        daTipoSubProd = New lbGlobalcom.dsGlobalTableAdapters.GLO_DATOSTABLASTableAdapter
        Try
            daTipoSubProd.FillByIdGrupo(DsGlobal.GLO_DATOSTABLAS, "sp")
        Catch ex As Exception
            Throw ex
        End Try
    End Sub

    Private Sub fListaArtbyNotaDev_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        CargaDatosSubProd()
    End Sub
    
    Private Sub MarcaPadresEHijos(ByVal dtPPack As lbGlobalcom.GetDatosPrepack)
        For Each drPPack As lbGlobalcom.dsInvenConsultas.INV_GETPREPACKRow In dtPPack.Prepack.Rows
            Dim drInfNCP As lbGlobalcom.dsConsultaMovimi.INFONOTASCREDITORow = Nothing
            If Not drPPack.IsTIPOSUBPROPNull Then _
                drInfNCP = dsConMov.INFONOTASCREDITO.Rows.Find(New Object() {drPPack.TIPOSUBPROP, drPPack.NUMEROREGISTROP})
            Dim drInfNCH As lbGlobalcom.dsConsultaMovimi.INFONOTASCREDITORow = Nothing
            If Not drPPack.IsNUMEROREGISTROHNull AndAlso Not drPPack.IsTIPOSUBPRODHNull Then _
                drInfNCH = dsConMov.INFONOTASCREDITO.Rows.Find(New Object() {drPPack.TIPOSUBPRODH, drPPack.NUMEROREGISTROH})
            If drInfNCH IsNot Nothing Then
                drInfNCH.PAREJA = drPPack.PAREJA
                drInfNCH.EsPrepack = Not drInfNCH.EsPrepack
                drInfNCH._SELECT = Not drInfNCH._SELECT
            End If
            If drInfNCP IsNot Nothing Then
                drInfNCP.PAREJA = drPPack.PAREJA
                drInfNCP.EsPrepack = Not drInfNCP.EsPrepack
                drInfNCP._SELECT = Not drInfNCP._SELECT
            End If
        Next
    End Sub
    Private Sub INFONOTASCREDITODataGridView_CellClick(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles INFONOTASCREDITODataGridView.CellClick
        Select Case e.ColumnIndex
            Case 7
                Dim dtPPack As lbGlobalcom.GetDatosPrepack = New lbGlobalcom.GetDatosPrepack(INFONOTASCREDITODataGridView.CurrentRow.Cells("IDPRODUCTO").Value, INFONOTASCREDITODataGridView.CurrentRow.Cells("TIPOSUBPROD").Value, INFONOTASCREDITODataGridView.CurrentRow.Cells("NUMEROREGISTRO").Value)
                If dtPPack.EsPrepack And dtPPack.PPackEsPadre Then
                    MarcaPadresEHijos(dtPPack)
                Else
                    MessageBox.Show("Este producto pertenece a un prepack y es hijo, por lo tanto no se puede seleccionar, favor buscar su padre", "Error en selección", MessageBoxButtons.OK, MessageBoxIcon.Stop)
                    INFONOTASCREDITODataGridView.EndEdit()
                End If
        End Select
    End Sub
End Class